#include <stdio.h>
#include <stdlib.h>
#define INITIAL_CAPACITY 10

typedef struct {
    int *data;     // 存储数据的数组
    int size;      // 当前元素个数
    int capacity;  // 当前容量
} SeqList;


// 初始化顺序表
void initSeqList(SeqList *list) {
    list->data = (int *)malloc(INITIAL_CAPACITY * sizeof(int));
    list->size = 0;
    list->capacity = INITIAL_CAPACITY;
}

// 扩容函数
void resizeSeqList(SeqList *list) {
    int newCapacity = list->capacity * 2;  // 将容量扩大一倍
    int *newData = (int *)realloc(list->data, newCapacity * sizeof(int));
    if (newData == NULL) {
        fprintf(stderr, "Memory allocation failed\n");
        exit(1);
    }
    list->data = newData;
    list->capacity = newCapacity;
}

// 添加元素到顺序表
void addElement(SeqList *list, int element) {
    if (list->size == list->capacity) {
        resizeSeqList(list);
    }
    list->data[list->size++] = element;
}

// 打印顺序表
void printSeqList(SeqList *list) {
    for (int i = 0; i < list->size; i++) {
        printf("%d ", list->data[i]);
    }
    printf("\n");
}

// 释放顺序表
void freeSeqList(SeqList *list) {
    free(list->data);
    list->data = NULL;
    list->size = list->capacity = 0;
}

int main() {
    SeqList list;
    initSeqList(&list);

    for (int i = 0; i < 20; i++) {
        addElement(&list, i);
    }

    printSeqList(&list);
    freeSeqList(&list);

    return 0;
}